package com.xiaotu.spider.project.pageprocessor.list;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.*;
import com.xiaotu.spider.AUrlPageProcessor;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.List;

/**
 * 爬取猫眼年度票房榜单数据
 */
@Component("MaoYanYearRankPageProcessor")
public class MaoYanYearRankPageProcessor extends AUrlPageProcessor
{
    private static final String URL = "http://piaofang.maoyan" +
            ".com/rankings/year?year=%s&limit=100&tab=%s";

    private static final String[] YEARS = DateUtils.getRecentYears(5);//最近5年

    private static final String[] TABS = new String[]{"1", "2", "3", "4", "5"};

    private static final String HOST = "http://piaofang.maoyan.com";

    @Override
    public void process(Page page)
    {
        if (page.getUrl().toString().contains("/rankings/"))
            this.listProcess(page);
        else
            this.detailProcess(page);
    }

    public void listProcess(Page page)
    {
        String year = StringUtil.nullToString(page.getRequest().getExtra("year"));
        try
        {
            List<String> movList = page.getHtml().xpath
                    ("//div[@id='ranks-list']/ul[@class='row']").all();
            if (movList == null || movList.isEmpty())
                throw new SpiderException(SpiderExceptionCode.DataInvalideException);
            Html html = PageUtils.getFontData(page);
            movList = html.xpath("//div[@id='ranks-list']/ul[@class='row']").all();
            movList.forEach(liStr ->
            {
                Html liObj = new Html(liStr);
                String order = liObj.xpath("//li[@class='col0']/text()").get();
                String name = liObj.xpath("//li[@class='col1']/p/text()").get();
                String boxOffice = liObj.xpath("//li[@class='col2 tr']/i/text()").get() + "万";
                String avgSell = liObj.xpath("//li[@class='col3 tr']/i/text()").get();
                String avgNum = liObj.xpath("//li[@class='col2 tr']/i/text()").get();
                String date = liObj.xpath("//li[@class='col1']/p[@class='second-line']/text()")
                        .get().substring(0, 10);
                String url = liObj.xpath("//ul/@data-com").regex("/movie/" + RegexUtils
                        .REGEX_INTEGER).get();
                System.out.println(StringUtils.join(new String[]{order, name, boxOffice, avgSell,
                        avgNum, date}, SepratorUtil.SEP_CSV_ARRAY));
                PageUtils.addTargetRequest(page, HOST + url, PageUtils.getNewExtraMapFromPage
                        (page, new String[]{"info", "year"}, new Object[]{new String[]{order,
                                name, boxOffice, avgSell, avgNum, date}, year}));
            });
        }
        catch (SpiderException e)
        {
            e.printStackTrace();
            FileUtil.writeFile("D://maoyan/" + year + "_listerr.txt", page.getUrl().toString() +
                    SepratorUtil
                            .LINE_SEPRATOR + e.getMessage() + SepratorUtil.LINE_SEPRATOR);
        }
    }

    private void detailProcess(Page page)
    {
        try
        {
            Html html = page.getHtml();
            String year = (String) page.getRequest().getExtra("year");
            String[] listInfo = (String[]) page.getRequest().getExtra("info");
            String result = page.getRawText();
            if (StringUtil.isEmpty(result))
                throw new SpiderException(SpiderExceptionCode.DataInvalideException);
            String score = html.xpath("//p[@class='score-num ']/i/text()").get();
            String area = html.xpath("//p[@class='info-category']/text()").get();

            String line = year + SepratorUtil.SEP_CSV_FIELDS + StringUtils.join(listInfo,
                    SepratorUtil.SEP_CSV_FIELDS) + SepratorUtil.SEP_CSV_FIELDS + area +
                    SepratorUtil.SEP_CSV_FIELDS + score;
            System.out.println(line);
            FileUtil.writeFile("D://maoyan/" + year + ".csv", line + SepratorUtil.LINE_SEPRATOR);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            FileUtil.writeFile("D://maoyan/detailerr.txt", page.getUrl().toString());
        }
    }

    @Override
    protected String[] getStartRequestUrls()
    {
        return null;
    }

    @Override
    protected List<JSONObject> getStartRequestUrlsAndParams()
    {
        List<JSONObject> list = new ArrayList<>();
        for (int i = 0; i < YEARS.length; i++)
        {
            JSONObject target = new JSONObject();
            target.put("year", YEARS[i]);
            target.put(URL_KEY, String.format(URL, YEARS[i], TABS[i]));
            list.add(target);
        }
        return list;
    }

    @Override
    protected String getSpiderType()
    {
        return "MaoYanYearRank";
    }

    @Override
    public String getPageDataType(Page page)
    {
        return Constants.PageDataType.SEARCH_LIST;
    }
}
